<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改认证源')"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-auth-edit" th:object="${auth}">
        <input id="authId" name="authId" type="hidden" th:field="*{authId}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">认证源名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authName" id="authName" th:field="*{authName}" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">认证源类型：</label>
            <div class="col-sm-8">
                <select disabled name="type" class="form-control m-b"
                        th:with="type=${@dict.getType('sys_auth_way')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{type}"></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">client_name：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="clientName" id="clientName" th:field="*{clientName}"
                       required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">认证地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="authUrl" th:field="*{authUrl}" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 认证服务地址</span>
            </div>
        </div>

        <div class="form-group" th:unless="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">CAS认证地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="authUrl" th:field="*{authUrl}" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> CAS服务端地址</span>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">当前访问地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="localServer" id="localServer" th:field="*{localServer}"
                       required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">获取token地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="tokenUrl" th:field="*{tokenUrl}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">用户信息地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="profileUrl" th:field="*{profileUrl}" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> profileUrl</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">用户账号字段：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="profileId" th:field="*{profileId}" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> profileId</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2'}">
            <label class="col-sm-3 control-label is-required">用户信息所在字段：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="authUrl" id="profilePath" th:field="*{profilePath}" required>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> profilePath</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2' || type == 'OAUTH2' || type == 'OIDC'}">
            <label class="col-sm-3 control-label is-required">client_id：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="clientId" id="clientId" th:field="*{clientId}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2' || type == 'OAUTH2' || type == 'OIDC'}">
            <label class="col-sm-3 control-label is-required">client_secret：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="clientSecret" id="clientSecret" th:field="*{clientSecret}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'GENERIC_OAUTH2' || type == 'OAUTH2' || type == 'OIDC'}">
            <label class="col-sm-3 control-label is-required">scope：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="scope" id="scope" th:field="*{scope}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'OIDC'}">
            <label class="col-sm-3 control-label is-required">well_known：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="wellKnown" id="wellKnown" th:field="*{wellKnown}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'JWT'}">
            <label class="col-sm-3 control-label is-required">签名密钥：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="signatureKey" id="signatureKey" th:field="*{signatureKey}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'JWT'}">
            <label class="col-sm-3 control-label is-required">加密密钥：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="encryptionKey" id="encryptionKey" th:field="*{encryptionKey}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label is-required">entity_id：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="entityId" id="entityId" th:field="*{entityId}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label is-required">元数据地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="metadataPath" id="metadataPath" th:field="*{metadataPath}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label is-required">元数据文件名：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="metadataFilename" id="metadataFilename" th:field="*{metadataFilename}" required>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">SAML密钥库：</label>
            <div class="col-sm-8">
                <select id="keystoreId" name="keystoreId" class="form-control" >
                    <option th:each="keystore:${keystores}" th:value="${keystore.keystoreId}" th:text="${keystore.keystoreName}"
                            th:field="*{keystoreId}"></option>
                </select>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">禁用签名校验：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="allSignatureValidationDisabled" th:checked="${auth.allSignatureValidationDisabled == 'Y'}">
                    <span></span>
                </label>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> allSignatureValidationDisabled</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">向idp发起认证请求时加签：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="authnRequestSigned" th:checked="${auth.authnRequestSigned == 'Y'}">
                    <span></span>
                </label>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> authnRequestSigned</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">向idp发起登出请求时加签：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="spLogoutRequestSigned" th:checked="${auth.spLogoutRequestSigned == 'Y'}">
                    <span></span>
                </label>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> spLogoutRequestSigned</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">要求idp下发凭据加签：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="wantsAssertionsSigned" th:checked="${auth.wantsAssertionsSigned == 'Y'}">
                    <span></span>
                </label>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> wantsAssertionsSigned</span>
            </div>
        </div>

        <div class="form-group" th:if="*{type == 'SAML'}">
            <label class="col-sm-3 control-label">要求idp响应信息加签：</label>
            <div class="col-sm-8">
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="wantsResponsesSigned" th:checked="${auth.wantsResponsesSigned == 'Y'}">
                    <span></span>
                </label>
                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> wantsResponsesSigned</span>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label is-required">成功跳转地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="successRedirectUrl" id="successRedirectUrl"
                       th:field="*{successRedirectUrl}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">回调地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="callbackUrl" id="callbackUrl"
                       th:field="*{callbackUrl}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">登出地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="logoutUrl" id="logoutUrl"
                       th:field="*{logoutUrl}">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">显示顺序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="sort" id="sort" th:field="*{sort}" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input id="remark" name="remark" class="form-control" type="text" th:field="*{remark}">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    var prefix = ctx + "system/auth";

    $("#form-auth-edit").validate({
        onkeyup: false,
        rules: {
            authName: {
                remote: {
                    url: prefix + "/checkAuthNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "authId": function () {
                            return $("#authId").val();
                        },
                        "authName": function () {
                            return $.common.trim($("#authName").val());
                        }
                    }
                }
            },
            clientName: {
                remote: {
                    url: prefix + "/checkClientNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "authId": function () {
                            return $("#authId").val();
                        },
                        "clientName": function () {
                            return $.common.trim($("#clientName").val());
                        }
                    }
                }
            },
            metadataFilename: {
                remote: {
                    url: prefix + "/checkMetadataFilenameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "authId": function () {
                            return $("#authId").val();
                        },
                        "metadataFilename": function () {
                            return $.common.trim($("#metadataFilename").val());
                        }
                    }
                }
            },
        },
        messages: {
            "authName": {
                remote: "认证源名称已经存在"
            },
            "clientName": {
                remote: "client_name已经存在"
            }
        },
        focusCleanup: true
    });

    function edit() {
        var type = $("select[name='type']").val();
        var authId = $("input[name='authId']").val();
        var authName = $("input[name='authName']").val();
        var clientName = $("input[name='clientName']").val();
        var authUrl = $("input[name='authUrl']").val();
        var clientId = $("input[name='clientId']").val();
        var clientSecret = $("input[name='clientSecret']").val();
        var scope = $("input[name='scope']").val();
        var wellKnown = $("input[name='wellKnown']").val();
        var signatureKey = $("input[name='signatureKey']").val();
        var encryptionKey = $("input[name='encryptionKey']").val();
        var entityId = $("input[name='entityId']").val();
        var metadataPath = $("input[name='metadataPath']").val();
        var metadataFilename = $("input[name='metadataFilename']").val();
        var keystoreId = $("select[name='keystoreId']").val();
        var allSignatureValidationDisabled = $("input[id='allSignatureValidationDisabled']").is(':checked') ? 'Y' : 'N';
        var authnRequestSigned = $("input[id='authnRequestSigned']").is(':checked') ? 'Y' : 'N';
        var spLogoutRequestSigned = $("input[id='spLogoutRequestSigned']").is(':checked') ? 'Y' : 'N';
        var wantsAssertionsSigned = $("input[id='wantsAssertionsSigned']").is(':checked') ? 'Y' : 'N';
        var wantsResponsesSigned = $("input[id='wantsResponsesSigned']").is(':checked') ? 'Y' : 'N';


        var successRedirectUrl = $("input[name='successRedirectUrl']").val();
        var localServer = $("input[name='localServer']").val();
        var callbackUrl = $("input[name='callbackUrl']").val();
        var logoutUrl = $("input[name='logoutUrl']").val();
        var sort = $("input[name='sort']").val();
        var remark = $("input[name='remark']").val();

        var tokenUrl = $("input[name='tokenUrl']").val();
        var profileUrl = $("input[name='profileUrl']").val();
        var profileId = $("input[name='profileId']").val();
        var profilePath = $("input[name='profilePath']").val();

        var data = {
            authId,
            authName,
            clientName,
            authUrl,
            successRedirectUrl,
            localServer,
            callbackUrl,
            logoutUrl,
            sort,
            remark
        }

        if (['GENERIC_OAUTH2', 'OAUTH2', 'OIDC'].includes(type)) {
            data = {
                ...data,
                clientId,
                clientSecret,
                scope,
            }
        }

        if (['GENERIC_OAUTH2'].includes(type)) {
            data = {
                ...data,
                tokenUrl,
                profileUrl,
                profileId,
                profilePath,
            }
        }

        if (['OIDC'].includes(type)) {
            data = {
                ...data,
                wellKnown,
            }
        }

        if (['SAML'].includes(type)) {
            data = {
                ...data,
                entityId,
                metadataPath,
                metadataFilename,
                keystoreId,
                allSignatureValidationDisabled,
                authnRequestSigned,
                spLogoutRequestSigned,
                wantsAssertionsSigned,
                wantsResponsesSigned,
            }
        }

        if (['JWT'].includes(type)) {
            data = {
                ...data,
                signatureKey,
                encryptionKey
            }
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/edit",
            data,
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }
</script>
</body>
</html>
